﻿using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace CookieDemo.Web.Controllers
{
    public class AccountController : Controller
    {
        public async Task<IActionResult> GenerateCookie(string userName = "9527")
        {
            var claims = new List<Claim>()
            {
                new Claim("id", "1"),
                new Claim(ClaimTypes.Name,userName),
                new Claim(ClaimTypes.Role,"Admin"),
            };
            var claimsIdentity = new ClaimsIdentity(claims, "Custom");
            var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);

            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, new AuthenticationProperties
            {
                ExpiresUtc = DateTime.Now.AddMinutes(30)
            });

            return Content(HttpContext.Response.Headers.SetCookie);
        }
    }
}